using DYH.Extensions.CAD.Model;
using DYH.Extensions.CAD.Service;
using DYH.Extensions.CAD.Service.Interface;

namespace DYH.Extensions.CAD;

/// <summary>
/// 提供初始化功能的静态类，用于配置和设置程序集
/// </summary>
public static class Initialize
{
    /// <summary>
    /// 配置
    /// </summary>
    internal static DYHCadSetupConfig Config { get; private set; } = new();

    /// <summary>
    /// 初始化配置并设置程序集列表
    /// </summary>
    /// <param name="action">用于配置DYHCadSetupConfig的操作</param>
    public static void Setup(Action<DYHCadSetupConfig> action)
    {
        var config = new DYHCadSetupConfig();
        action.Invoke(config);
        Config = config;
        SetAssemblies();
        AppHelper.Init();
        var userSettingService = AppHelper.Get<IUserSettingService>();
        userSettingService.ReloadAll();
    }

    /// <summary>
    /// 设置程序集列表，确保包含当前程序集
    /// </summary>
    private static void SetAssemblies()
    {
        var list = Config.Assemblies.Concat([typeof(Initialize).Assembly]).ToHashSet().ToList();
        Config.Assemblies = list;
    }
}